package com.iamteer.day18.dao;

import java.util.ArrayList;
import java.util.Iterator;

import com.iamteer.day18.model.Book;

public class BookDao {
	private static ArrayList<Book> books = new ArrayList<Book>();
	
	public ArrayList<Book> listBooks() {
//		System.out.println(books);
		return books;
	}
	
	public boolean addBook(Book book) {
//		System.out.println(books);
		return books.add(book);
	}
	
	public boolean editBook(Book book) {
		boolean isSuccessful = false;
		
		
		return isSuccessful;
	}
	
	public boolean deleteBook(Book book) {
		boolean isSuccessful = false;
		String bookName = book.getName();
		String bookAuthor = book.getAuthor();
		String bookIsbn = book.getIsbn();
		Iterator<Book> iterator = books.iterator();
		while (iterator.hasNext()) {
			Book book2 = (Book) iterator.next();
			if ( bookName != null) {
				if (book2.getName().equals(bookName)) {
					books.remove(book2);
					isSuccessful = true;
					break;
				}
			} else if (bookAuthor !=null) {
				if (book2.getAuthor().equals(bookAuthor)) {
					books.remove(book2);
					isSuccessful = true;
					break;
				}
			} else if (bookIsbn !=null) {
				if (book2.getIsbn().equals(bookIsbn)) {
					books.remove(book2);
					isSuccessful = true;
					break;
				}
			}
		}
		
		return isSuccessful;
	}
	
	public Book findBook(String isbn){
		Iterator<Book> iterator = books.iterator();
		while (iterator.hasNext()) {
			Book book = (Book) iterator.next();
			if (book.getIsbn().equals(isbn)) {
				return book;
			}
		}
		return null;
	}
}
